const app = getApp();
const util = require('../../utils/util');
const config = require('../../common/config');

Page({
    data: {
        userInfoTypeContent:'',
        userInfoType:''
    },
    onLoad: function (opt) {
        wx.setNavigationBarTitle({
            title: '个人信息修改'
        })
        var _this = this;
        this.setData({
            userInfoType:opt.userInfoType
        })
        if (app.globalData.userInfo != null) {
            this.setData({
                userInfoTypeContent : app.globalData.userInfo[opt.userInfoType]
            })
        }
    },
    goBack:function () {
        var _this = this;
        const userId = wx.getStorageSync(config.USER_ID);

        if(app.globalData.userInfo[_this.data.userInfoType] == _this.data.userInfoTypeContent){
            util.debug("userinfomanagejs:信息未变更");
            wx.navigateBack({
                delta: 1
            })
            return;
        }

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'userService',
                methodName: 'updateUserInfo',
                reqData:JSON.stringify({
                    userId:userId,
                    content:_this.data.userInfoTypeContent,
                    type:_this.data.userInfoType
                })
            },
            success: function (res) {
                var resultData = res.data;
                if(resultData.code == 1){
                    util.debug("userinfomanagejs:用户主表更新成功");
                    app.globalData.userInfo[_this.data.userInfoType] = _this.data.userInfoTypeContent
                    var pages= getCurrentPages();
                    var prevPage = pages[pages.length - 2];
                    if(_this.data.userInfoType == 'realName'){
                        prevPage.setData({
                            realName:_this.data.userInfoTypeContent
                        })
                    }
                    if(_this.data.userInfoType == 'mobile'){
                        prevPage.setData({
                            mobile:_this.data.userInfoTypeContent
                        })
                    }
                    if(_this.data.userInfoType == 'cardNum'){
                        var nCardNum = _this.data.userInfoTypeContent;
                        if(nCardNum.length>4 && nCardNum.length<18){
                            nCardNum = nCardNum.substring(0,3) + "********";
                        }
                        if(nCardNum.length>=18){
                            nCardNum = nCardNum.substring(0,3) + "********" +nCardNum.substring(14);
                        }
                        prevPage.setData({
                            cardNum:nCardNum
                        })
                    }
                    if(_this.data.userInfoType == 'address'){
                        prevPage.setData({
                            address:_this.data.userInfoTypeContent
                        })
                    }

                    wx.navigateBack({
                        delta: 1
                    })
                }else{
                    util.debug("userinfomanagejs:用户主表更新失败");
                }
            }
        })
    },
    bindContentInput:function (e) {
        this.setData({
            userInfoTypeContent: e.detail.value
        })
    }

})
